// Validator Object
var valid = new Object();	// REGEX Elements
// Match Integer
valid.Integer = /^(\d)+$/;
valid.Float = /^(\d)+(.(\d)+)*$/;
//Match Name
valid.Name = /^(\w)+$/;
// matches 05:04 or 12:34 but not 75:83
valid.Time24 = /^(([0-1][0-9])|(2[0-4])):[0-5][0-9]$/;
//matches email
valid.Email = /^(\w)+([.](\w)+)*@(\w)+([.](\w)+)+$/;
// Match mm/yyyy
valid.MonthYear = /^(0[1-9])|(1[0-2]){1}\/\d{4}$/;
// Match dd/mm/yyyy
valid.DayMonthYear = /^(([1-9]|0[1-9]|([1-2][0-9])|3[0-1]))\/(([1-9])|(0[1-9])|(1[0-2]))\/\d{4}$/;
// Match dd/mm/yyyy H:i:s
valid.FullTime = /^((0[1-9]|([1-2][0-9])|3[0-1]))\/((0[1-9])|(1[0-2]))\/\d{4} (0[0-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])$/;
// Match Phone number
valid.Phone = /^(\d)+(\d|.|-)*(\d)$/;
// Match mobi phone full
valid.MobiPhone = /^09[0|1|3|5|8]\d{7}$/;
valid.BillNo = /^(\w)+([.-](\w)+)*$/;

function Validator(sFormID)
{
	// ID cua form can check
	this.FormID = sFormID;
	// Danh sach cac field can validate
	this.Fields = new Array();
	// Cac ham
	this.AddValidation = _AddValidation;
	this.AddRequired = _AddRequired;
	this.RemoveRequired = _RemoveRequired;
	this.CheckForm = _CheckForm;
}

function _AddValidation(ID, sValidator)
{
	// Kiem tra neu chua co doi tuong thi tao doi tuong cho field
	if(!this.Fields[ID])
	{
		this.Fields[ID] = new Object();
	}
	this.Fields[ID].v = sValidator;
	return;
}

function _AddRequired(ID)
{
	if(!this.Fields[ID])
	{
		this.Fields[ID] = new Object();
	}
	this.Fields[ID].r = 1;
	return;
}

function _RemoveRequired(ID)
{
	if(!this.Fields[ID])
	{
		return(false);
	}

	if(this.Fields[ID].r == 1)
	{
		this.Fields[ID].r = 0;
	}
	return(true);
}

function _CheckForm(bMark)
{
	var tmpObj, tmpVal;
	tmpObj = document.getElementById(this.FormID);
	// Neu khong co doi tuong form thi khoi kiem tra
	if(!tmpObj)
	{
		alert("Debug: Form is not existed");
		return(false);
	}

	var ret = true;

	for(key in this.Fields)
	{
		tmpObj = document.getElementById(key);
		// Truong hop khong co doi tuong thi bo qua
		if(!tmpObj)
		{
			//alert(key + ' khong co doi tuong: bo qua');
			continue;
		}

		tmpObj.className = "input";

		tmpVal = tmpObj.value;
		if((tmpVal == null) || (tmpVal == ""))
		{
			// Truong hop khong nhap du lieu
			if( (this.Fields[key].r) && (this.Fields[key].r == 1) )
			{
				// Truong hop bat buoc nhung khong nhap
				//alert('Bat buoc nhap');
				tmpObj.className = "invalidated";
				if(ret != false)
				{
					tmpObj.focus();
					//tmpObj.select();
					ret = false;
				}

			}// Het truong hop bat buoc nhap lieu
		}// Het truong hop khong nhap lieu vao doi tuong
		else
		{
			// Truong hop co nhap du lieu
			v = this.Fields[key].v;
			if( (v) && (valid[v]) )
			{
				// Truong hop ton tai validator
				if(!valid[v].exec(tmpVal))
				{
					// Truong hop kiem tra sai cu phap
					tmpObj.className = "invalidated";
					if(ret != false)
					{
						tmpObj.focus();
						//tmpObj.select();
						ret = false;
					}
				}// Het truong hop kiem tra bi sai cu phap
			}// Het truong hop ton tai validator
		}
	}
	return(ret);
}
